package com.trxy.hotelsystem.contorller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.kaifanRecord;
import com.trxy.hotelsystem.service.impl.KaifanRecordServiceImpl;

@Controller
public class KaifanRecordController {
	
	@Autowired
	private KaifanRecordServiceImpl KaifanRecordServiceimpl;
	
	//会员查询自己的开房记录
	@RequestMapping(value = "selectKaifanRecord.do")
	public String selectKaifanRecord(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			Model model,clientInf clientinf,kaifanRecord kaifanrecord,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		clientInf us = (clientInf) session.getAttribute("viplogin");
		
		List<kaifanRecord> list = KaifanRecordServiceimpl.selectkaifanRecordByVipNumber(us.getNumber());
		
		for(kaifanRecord one:list) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String statime = formatter.format(one.getStatime());
			 one.setReserved2(statime);
			 if(one.getEndtime() != null) {
				 String endtime = formatter.format(one.getEndtime());
				 one.setReserved3(endtime);
			 }	 
		}
		
		PageInfo<kaifanRecord> page = new PageInfo<>(list);
		
	    model.addAttribute("pageInfo", page);
		
		return "view/vipview/checkinRecord";
	}
	
	//查询所有的开房记录
	@RequestMapping(value = "queryKaifangRecordAll.do")
	public String queryKaifangRecordAll(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			Model model,clientInf clientinf,kaifanRecord kaifanrecord,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<kaifanRecord> list = KaifanRecordServiceimpl.queryKaifangRecordAll();
		
		for(kaifanRecord one:list) {
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String statime = formatter.format(one.getStatime());
			 one.setReserved2(statime);
			 if(one.getEndtime() != null) {
				 String endtime = formatter.format(one.getEndtime());
				 one.setReserved3(endtime);
			 }	 
			
		}
		
	    PageInfo<kaifanRecord> page = new PageInfo<>(list);
		
	    model.addAttribute("pageInfo", page);
		
		
		return "view/kaifan/kaifanrecordMain";
	}
	
	//条件查询开房记录
	@RequestMapping(value = "selectKaifanRecord2.do")
	public String selectKaifanRecord(HttpServletRequest request, HttpServletResponse response,Model model,
			clientInf clientinf,kaifanRecord kaifanrecord,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String vipNumber = request.getParameter("vipNumber");
		String vipName = request.getParameter("vipName");
		String vipIdNumber = request.getParameter("vipIdNumber");
		
		kaifanrecord.setNumber(vipNumber);
		kaifanrecord.setName(vipName);
		kaifanrecord.setIdnumber(vipIdNumber);
		
		List<kaifanRecord> list = KaifanRecordServiceimpl.selectKaifanRecord(kaifanrecord);
		
		for (kaifanRecord one : list) {

			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String statime = formatter.format(one.getStatime());
			one.setReserved2(statime);
			if (one.getEndtime() != null) {
				String endtime = formatter.format(one.getEndtime());
				one.setReserved3(endtime);
			}

		}
		
		PageInfo<kaifanRecord> page = new PageInfo<>(list);

		model.addAttribute("pageInfo", page);
		model.addAttribute("vipNumber", vipNumber);
		model.addAttribute("vipName", vipName);
		model.addAttribute("vipIdNumber", vipIdNumber);
		model.addAttribute("inf", "多条件");
		
		
		return "view/kaifan/kaifanrecordMain";
	}

}
